from pdf2image import convert_from_path
import os

def convert_pdf_to_jpg(pdf_file):
    base_name = os.path.splitext(os.path.basename(pdf_file))[0]
    output_folder = os.path.dirname(pdf_file)
    try:
        pages = convert_from_path(pdf_file)
        for i, page in enumerate(pages):
            output_filename = f"{base_name}_page_{i+1}.jpg"
            output_path = os.path.join(output_folder, output_filename)
            page.save(output_path, "JPEG")
    except Exception as e:
        print(f"Error converting {pdf_file}: {e}")

def process_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith('.pdf'):
                pdf_file_path = os.path.join(root, file)
                convert_pdf_to_jpg(pdf_file_path)

# 使用示例
directory = 'E:\\小象第二批\\zhungeer2.7z\\准格尔金诚2\\4.长银消金-3699笔-齐\\长银-放款流水、代偿证明、债转协议-齐\\长银-债转协议（中智信、钧信与象源）\\债转'  # 替换为你的目录路径

process_directory(directory)
